package com.beijing.demo.sort;

import java.util.Arrays;

/**
 * 曹旭
 * 2025/4/21
 * 练习三傻排序 : 冒泡  选择  插入
 */
public class SortTest {

    public static void main(String[] args) {
        System.out.println("test start~~");
        int count = 10000000;
        int len = 30;
        int value = 300;
        for (int i = 0; i < count; i++) {
            int[] arr1 = getRandomArr(len,value);
            int[] arr2 = Arrays.copyOf(arr1,arr1.length);
//            int[] bubbleSort = bubbleSort(arr1);
            int[] bubbleSort = insertSort(arr1);
//            int[] bubbleSort = selectSort(arr1);
            Arrays.sort(arr2);
            if(!equalsArr(bubbleSort,arr2)){
                System.out.println("fucking fuck~~~");
                break;
            }
        }
        System.out.println("test end~~");
    }


    /**
     * 实现选择排序
     * @param arrs
     * @return
     */
    public static int[] selectSort(int[] arrs){
        if(arrs == null || arrs.length < 2){
            return arrs;
        }
        for (int i = 0; i < arrs.length; i++) {
            int minIndex = i;
            for (int j = i; j < arrs.length; j++) {
                if(arrs[minIndex] > arrs[j]){
                   minIndex = j;
                }
            }
            swap(arrs,i,minIndex);
        }
        return arrs;
    }

    /**
     * 实现插入排序
     * @param arrs
     * @return
     */
    public static int[] insertSort(int[] arrs){
        if(arrs == null || arrs.length < 2){
            return arrs;
        }
        for (int i = 0; i < arrs.length - 1; i++) {
            for (int j = i; j >= 0 && arrs[j + 1] < arrs[j]; j--) {
                swap(arrs,j,j+1);
            }
        }
        return arrs;
    }

    /**
     * 对比两个int数组是否完全一样
     * @return
     */
    public static boolean equalsArr(int[] arr1,int[] arr2){
        if(arr1 == null && arr2 == null){
            return true;
        }
        if((arr1 == null || arr2 == null) && arr1 != arr2){
            return false;
        }
        if(arr1.length != arr2.length){
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if(arr1[i] != arr2[i]){
                return false;
            }
        }
        return true;
    }

    /**
     * 实现冒泡排序
     * @param arr
     * @return
     */
    public static int[] bubbleSort(int[] arr){
        if(arr == null || arr.length < 2){
            return arr;
        }
        for (int i = arr.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if(arr[j + 1] < arr[j]){
                    swap(arr,j+1,j);
                }
            }
        }
        return arr;
    }

    public static void swap(int[] arr, int n, int m){
        int temp = arr[n];
        arr[n] = arr[m];
        arr[m] = temp;
    }

    /**
     * 生成随机数组
     * len [1,len]
     * maxValue [-maxValue,maxValue]
     * @param len
     * @param maxValue
     * @return
     */
    public static int[] getRandomArr(int len,int maxValue){
        int arrLen = (int) (Math.random() * len) + 1;
        int[] arr = new int[arrLen];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = getRandomNum(maxValue);
        }
        return arr;
    }

    /**
     * 获取随机数,随机数的范围在[-value,value]之间
     * @param value
     * @return
     */
    public static int getRandomNum(int value){
        return (int)(Math.random() * (value + 1)) - (int)(Math.random() * (value + 1));
    }
}
